#!/usr/bin/env python
# -*—coding:utf8-*-
# @author: flame
# @file: calculator.py
# @time: 2023/10/10 14:31

def calculator(num1,num2,operator):
    if operator == '+':
        result = num1 + num2
    elif operator == '-':
        result = num1 - num2
    elif operator == '*':
        result = num1 * num2
    elif operator == '/':
        if num2 != 0:
            result = num1 / num2
        else:
            result = "发生错误：被除数不能为零"
    else:
        result = "发生错误：无效运算符"

    return result

while 1:
    try:
        print('------计算开始------')
        operator = input('请输入操作方式(+、-、*、/)中的一种:')
        num1 = float(input('请输入第一个数:'))
        num2 = float(input('请输入第二个数:'))

        result1 = calculator(num1, num2, operator)
        print(result1)
        flag = input('是否继续运算？(Y/N，不区分大小写)')
        if flag == 'N' or flag == 'n':
            break
    except ValueError:
        print('请输入有效数字')
    except Exception as e:
        print('程序发生异常:',e)
        break
    finally:
        print('------本次计算结束------')

